//
//  YKTabBarViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/9/24.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKTabBarViewController.h"
#import "LSLoginViewController.h"
#import "CommonDataManager.h"
#import "YKBusinessViewController.h"
#import "YKMineViewController.h"
#import "YKDataViewController.h"
#import "YKHomeViewController.h"
#import "YKApproveViewController.h"
#import "YKDataPageViewController.h"
#import "YKNormalDatePageViewController.h"
static YKTabBarViewController* vc = nil;
@interface YKTabBarViewController ()<LoginViewControllerDelegate>

@end

@implementation YKTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    vc = self;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onLoginTimeout:) name:@"LOGIN_TIMEOUT" object:nil];
}

- (void)onLoginTimeout:(id)sender {
    self.userModel = nil;
    [self promoteLogin];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if (!self.userModel) {
        [self promoteLogin];
    }
}
+ (instancetype)sharedInstance {
    return vc;
}
-(void)promoteLogin{
    LSLoginViewController* loginVC = [[LSLoginViewController alloc]init];
    loginVC.hidesBottomBarWhenPushed = YES;
    loginVC.delegate = self;
    loginVC.register_id = self.register_id;
    UINavigationController* navi = [[UINavigationController alloc]initWithRootViewController:loginVC];
    [self presentViewController:navi animated:YES completion:nil];
}

- (void)loginViewDidLoginSuccessWithUser:(YKUserModel *)userModel {
    self.userModel = userModel;
    UINavigationController *vc = (UINavigationController*)[self.viewControllers objectAtIndex:2];
    if (self.userModel.ISGLY == 1) {
        YKNormalDatePageViewController* vc4 = [[YKNormalDatePageViewController alloc]init];
        [vc setViewControllers:@[vc4] animated:YES];
        self.selectedIndex = 0;
    }
    else if(self.userModel.ISGLY == 2){
        YKDataPageViewController* vc4 = [[YKDataPageViewController alloc]init];
        [vc setViewControllers:@[vc4] animated:YES];
        self.selectedIndex = 2;
    }
    vc = (UINavigationController *)[self.viewControllers objectAtIndex:1];
    TFPageViewController * page = vc.viewControllers.firstObject;
    [page scrollToPage:0];
//    [page viewWillAppear:YES];
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    [TFHTTPMANAER POST:@"customerDataManager/searchCategorys.htm" parameters:nil cacheDuration:60 *60 *24 *7 success:^(id responseObject) {
        [CommonDataManager sharedInstance].customerCategoryList = [NSArray yy_modelArrayWithClass:[YKCustomerCategoryModel class] json:responseObject[@"customerCategoryList"]];
        for (YKCustomerCategoryModel *model in [CommonDataManager sharedInstance].customerCategoryList) {
            for (YKCustomerCategoryModel *m in model.subMap) {
                m.subMap = nil;
            }
        }
    } failure:^(id responseObject, NSError *error) {
        
    }];
    
    [TFHTTPMANAER POST:@"customerDataManager/searchRegions.htm" parameters:nil cacheDuration:60 *60 *24 *7 success:^(id responseObject) {
        [CommonDataManager sharedInstance].customerRegionList = [NSArray yy_modelArrayWithClass:[YKCustomerRegionModel class] json:responseObject[@"customerRegionList"]];
        for (YKCustomerRegionModel *model in [CommonDataManager sharedInstance].customerRegionList) {
            for (YKCustomerRegionModel *m in model.subMap) {
                m.subMap = nil;
            }
        }
    } failure:^(id responseObject, NSError *error) {
        
    }];
    
    [TFHTTPMANAER POST:@"manage/regions.htm" parameters:@{} cacheDuration:60 *60 *24 *30 success:^(id responseObject) {
        [CommonDataManager sharedInstance].regions = [NSArray yy_modelArrayWithClass:[YKRegionModel class] json:responseObject[@"regions"]];
        
    } failure:^(id responseObject, NSError *error) {
        NSLog(@"aaa");
    }];
    
    [TFHTTPMANAER POST:@"manage/goods.htm" parameters:@{} cacheDuration:60 *60 *24 success:^(id responseObject) {
        [CommonDataManager sharedInstance].goods = [NSArray yy_modelArrayWithClass:[YKSaleModel class] json:responseObject[@"goods"]];
        
    } failure:^(id responseObject, NSError *error) {
        NSLog(@"aaa");
    }];
    
    [TFHTTPMANAER POST:@"manage/guests.htm" parameters:@{} cacheDuration:60 *60 *24 success:^(id responseObject) {
        [CommonDataManager sharedInstance].guests = [NSArray yy_modelArrayWithClass:[YKSaleModel class] json:responseObject[@"guests"]];
        
    } failure:^(id responseObject, NSError *error) {
        NSLog(@"aaa");
    }];
    
    [TFHTTPMANAER POST:@"manage/users.htm" parameters:@{} cacheDuration:60 *60 *24 success:^(id responseObject) {
        [CommonDataManager sharedInstance].users = [NSArray yy_modelArrayWithClass:[YKSaleModel class] json:responseObject[@"users"]];
        
    } failure:^(id responseObject, NSError *error) {
        NSLog(@"aaa");
    }];
}


- (void)loginCancled {
    
}


@end
